package com.brogent;

import com.brogent.shapes.*;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ImageView;

public class PainterView extends ImageView {
    public Bitmap       mBitmap;
    private Bitmap      mShapeBitmap;
    private Canvas      mCanvas, mShapeCanvas;

    private int         height, width;
    private byte        mToolType = Shape.PEN;
    private Shape       mShape;

    public static Paint mPaint    = new Paint();

    public PainterView(Context c) {
        super(c);
        Log.i(getClass().getSimpleName(), "PainterView()");

        mShape = new LineShape();

        /** get the width and height of the screen */
        DisplayMetrics dm = new DisplayMetrics();
        ((Activity) c).getWindowManager().getDefaultDisplay().getMetrics(dm);

        height = dm.heightPixels;
        width = dm.widthPixels;

        /** create a bitmap object depend on width and height of the screen */
        Bitmap newBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);

        Canvas tCanvas = new Canvas();
        Paint tPaint = new Paint();

        tCanvas.setBitmap(newBitmap);
        tPaint.setARGB(255, 255, 255, 255);
        tCanvas.drawPaint(tPaint);

        mBitmap = newBitmap;
        mCanvas = tCanvas;

        invalidate();

        tCanvas = new Canvas();
        newBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

        tCanvas.setBitmap(newBitmap);
        tPaint.setARGB(0, 255, 255, 255);
        tCanvas.drawPaint(tPaint);

        mShapeBitmap = newBitmap;
        mShapeCanvas = tCanvas;
    }

    public void setTool(byte toolType) {
        this.mToolType = toolType;
        switch (mToolType) {
            case Shape.PEN:
                mShape = new LineShape();
                break;
            case Shape.LINE:
                mShape = new BeeLineShape();
                break;
            case Shape.RECTANGLE:
                mShape = new RectShape();
                break;
            case Shape.CIRCLE:
                mShape = new CircleShape();
                break;
            default:
                break;
        }
    }

    public byte getTool() {
        return this.mToolType;
    }

    public void clear() {
        Log.i(getClass().getSimpleName(), "clear()");
        if (mCanvas != null) {
            Paint tPaint = new Paint();
            tPaint.setARGB(255, 255, 255, 255);
            mCanvas.drawPaint(tPaint);
            // mToolType = Shape.CLEAR;
            invalidate();
        }
    }

    protected void loadImage(String path) {
        Log.i(getClass().getSimpleName(), "loadImage()");

        Bitmap tBitmap = BitmapFactory.decodeFile(path);
        mCanvas.drawBitmap(tBitmap, 0, 0, null);
        tBitmap.recycle();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        Log.i(getClass().getSimpleName(), "onSizeChanged()");
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Log.i(getClass().getSimpleName(), "onDraw()");
        canvas.drawBitmap(mBitmap, 0, 0, null);
        canvas.drawBitmap(mShapeBitmap, 0, 0, null);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mShape.addStartPoint((int) event.getX(), (int) event.getY());
                break;
            case MotionEvent.ACTION_MOVE:
                mShape.addEndPoint((int) event.getX(), (int) event.getY());
                mShape.draw(mShapeCanvas, mPaint, event);
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                mShape.addEndPoint((int) event.getX(), (int) event.getY());
                mShape.onDraw(mCanvas, mShapeBitmap);
                invalidate();
                mShapeBitmap.eraseColor(0x00FFFFFF);
                break;
            default:
                break;
        }
        return true;
    }
}
